//
//  UserAlreadyExistViewController.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/8.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "UserAlreadyExistViewController.h"

@interface UserAlreadyExistViewController ()
{
    AFHTTPRequestOperationManager *afManager;
}
@end

@implementation UserAlreadyExistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //標題添加電話
    self.LB_TitlePhone.text = [NSString stringWithFormat:@"%@ 此號碼已被註冊。\n請問您是否註冊過此服務？",self.userPhoneNumber];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)toLogin:(id)sender {
    //檢查 防止註冊完進到這邊 又選擇登入 又再進去註冊 又再一次選擇登入 的無限迴圈 所以這裡設置了 只有曾經經過登入畫面 就讓它回到登入畫面去
    [self.navigationController.viewControllers enumerateObjectsUsingBlock:^(UIViewController *controller, NSUInteger idx, BOOL *stop) {
        if ([controller isKindOfClass:[LoginViewController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
            *stop = YES;
            
        }else if(idx == self.navigationController.viewControllers.count-1)
        {
            
            //跳到登入頁面
            LoginViewController *loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
            [self.navigationController pushViewController:loginVC animated:YES];
        }

    }];
}


- (IBAction)oneUse:(id)sender {
    
    
    NSString *userPhoneNumber = [NSString stringWithFormat:@"我們將以簡訊發送驗證碼，到這個手機：%@",self.userPhoneNumber];
    
    //帳號密碼都沒有問題 按了確定 觸發 delegate 事件
    [[[UIAlertView alloc]initWithTitle:@"確認手機號" message:userPhoneNumber delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確認",nil]show];

}

-(void)sendRegistration
{
    //國碼 帳號(電話) 密碼 wifi位置 還有先前取得的 token
    //測試開發用地
    NSDictionary *parameters = @{@"type":@"add",@"kind":@"1",@"secret":@"openfire",@"countryCode":self.ISO_Country_Code,@"phone":self.userPhoneNumber,@"password":DEFAULT_PASSWORD,@"mac":self.WifiSSID,@"token":self.token,@"force":@"Y"};
    
    afManager = [AFHTTPRequestOperationManager manager];
    
        afManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
        afManager.responseSerializer.acceptableContentTypes = [afManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
    //發送註冊 等待簡訊回應
    [afManager POST:MD_SERVER_API parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"JSON: %@", responseObject);
        
        NSString *responseResult = [NSString stringWithFormat:@"%@",responseObject[@"result"]];
        
        //成功
        if ([responseResult isEqualToString:@"0"]) {
            
            //loading 結束
            [self.AV_LoadingAlertView dismissWithClickedButtonIndex:0 animated:YES];
            
            //跳到簡訊輸入頁面
            
            [self toSMSViewController];
            
        }
        else
        {
            
            [self.AV_LoadingAlertView dismissWithClickedButtonIndex:0 animated:NO];
            [WinkAlertView _showAlertViewWithTitle:@"失敗" content:@"註冊失敗" cancelButtonTitle:@"確認" showTime:3.0f];

        }
        /*
         CODE_IllegalArgument = 1;
         CODE_TokenTimeOut = 2;
         CODE_UserAlreadyExists = 3;
         CODE_UserAlreadyExistsNotEnable = 4;
         CODE_UserNotFound = 6;
         */
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        
        [self.AV_LoadingAlertView dismissWithClickedButtonIndex:0 animated:NO];
        
        [WinkAlertView _showAlertViewWithTitle:@"失敗" content:@"網路連結失敗" cancelButtonTitle:@"確認" showTime:3.0f];
        
        
    }];
}
-(void)toSMSViewController
{
    SMSViewController *SMSVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SMSViewController"];
    SMSVC.token=self.token;
    SMSVC.WifiSSID=self.WifiSSID;
    SMSVC.ISO_Country_Code=self.ISO_Country_Code;
    SMSVC.userPhoneNumber=self.userPhoneNumber;
    
    [self.navigationController pushViewController:SMSVC animated:YES];
}
////重新發送簡訊
//-(void)resendSMS
//{
//    
//
//    
//    afManager = [AFHTTPRequestOperationManager manager];
//    
//    afManager.responseSerializer = [AFJSONResponseSerializer serializer];
//    
//    afManager.responseSerializer.acceptableContentTypes = [afManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
//
//    NSDictionary *parameters = @{@"type":@"sms",@"kind":@"1",@"secret":@"openfire",@"countryCode":self.ISO_Country_Code,@"phone":self.userPhoneNumber,@"token":self.token};
//    
//    //發送註冊 等待簡訊回應
//    [afManager POST:@"http://xmpp.mooredoll.com:9090/plugins/mooredoll/mooredollservice" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        NSLog(@"JSON: %@", responseObject);
//        
//        NSString *responseResult = [NSString stringWithFormat:@"%@",responseObject[@"result"]];
//        //成功
//        if ([responseResult isEqualToString:@"0"]) {
//            
//            [self.AV_LoadingAlertView dismissWithClickedButtonIndex:0 animated:NO];
//            
//            //跳到簡訊頁面
//            [self toSMSViewController];
//            
//        }else
//        {
//            [WinkAlertView _showAlertViewWithTitle:@"簡訊重新發送失敗" content:responseObject[@"msg"] cancelButtonTitle:@"確認" showTime:3.0f];
//            
//        }
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"Error: %@", error);
//        
//        [WinkAlertView _showAlertViewWithTitle:@"失敗" content:@"網路連結失敗" cancelButtonTitle:@"確認" showTime:3.0f];
//        
//        
//    }];
//    
//}
#pragma mark AlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
        if (buttonIndex==1) {
            
            //並且把等待的 alertView show 出來
            self.AV_LoadingAlertView = [[UIAlertView alloc]initWithTitle:@"註冊中" message:@"請等待..." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
            [self.AV_LoadingAlertView show];
            
            [self sendRegistration];
        }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
